﻿
//Author: Andy Nogueira
//Version: SilverPart 2.1
//Comment: Updated to support Silverligth 2
//    May/2010 - Fix issues with Firefox
//

using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls.WebParts;

namespace SilverPart
{
    
    public partial class SilverPartWebPart : WebPart
    {

        #region Constants

        //Constants to be used as default values on WebPart properties
        private const string DEFAULT_VERSION = "2.0";
        private const string DEFAULT_WIDTH = "300";
        private const string DEFAULT_HEIGHT = "300";
        private const string DEFAULT_BACKGROUND_COLOR = "White";
        private const bool DEFAULT_ENABLEHTMLACCESS = true;
        private const bool DEFAULT_WINDOWLESS = false;
        private const bool DEFAULT_GETCURRENTUSER = true;
        private const bool DEFAULT_GETCURRENTSITE = true;
        private const string DEFAULT_INSTALL_IMAGE = "http://go.microsoft.com/fwlink/?LinkID=108181";
        private const string DEFAULT_INSTALL_URL = "http://www.microsoft.com/silverlight/handlers/getsilverlight.ashx?v=2.0";

        private const string SL_VERSION10 = "1.0";
        private const string SL_VERSION20 = "2.0";

        
        public enum SilverlightVersion
        { 
            Version1,
            Version2
        }

        #endregion

        #region Private Fields

        //Show open pane message
        private LiteralControl setWebPartSettings;

        //Silverlight Control ID
        private string silverlightControlID;

        //Silverlight Control Host
        private string silverlightControlHost;

        //Silverlight XAML Source
        private string silverlightControlSource;

        //Silverlight JavaScript file
        private string silverlightControlJavaScriptFile;

        //Silverlight Version
        private string silverlightControlVersion;

        //Silverlight Background Color
        private string silverlightControlBackground;

        //Silverlight Control Height
        private string silverlightControlHeight;

        //Silverlight Control Width
        private string silverlightControlWidth;

        //Silverlight Control inplaceInstallPrompt property
        private string silverlightControlInitParams;

        //Silverlight Control enableHtmlAccess property
        private bool silverlightControlHtmlAccess;
        
        //Silverlight Control Windowless property
        private bool silverlightControlWindowless;

        //Show Silverlihgt Install Banner ?
        private bool silverlightInstallBanner;

        //Silverlight Control Installation Banner Image
        private string silverlightInstallImage;

        //Silverlight Control Installation URL
        private string silverlightInstallURL;

        //Pass Current User as Initialization Parameter
        private bool getCurrentUserAsInitParam;

        //Pass Current Site as Initialization Parameter
        private bool getCurrentSiteAsInitParam;

        #endregion

        #region Constructors

        //Default Constructor
        public SilverPartWebPart()
        {
            this.ExportMode = WebPartExportMode.All;

            //Initialize WebPart default properties
            silverlightControlWidth = SilverPart.SilverPartWebPart.DEFAULT_WIDTH;
            silverlightControlHeight = SilverPart.SilverPartWebPart.DEFAULT_HEIGHT;
            silverlightControlBackground = SilverPart.SilverPartWebPart.DEFAULT_BACKGROUND_COLOR;
            silverlightControlHtmlAccess = SilverPart.SilverPartWebPart.DEFAULT_ENABLEHTMLACCESS;
            silverlightControlWindowless = SilverPart.SilverPartWebPart.DEFAULT_WINDOWLESS;
            silverlightControlVersion = SilverPart.SilverPartWebPart.DEFAULT_VERSION;
            silverlightInstallImage = SilverPart.SilverPartWebPart.DEFAULT_INSTALL_IMAGE;
            silverlightInstallURL = SilverPart.SilverPartWebPart.DEFAULT_INSTALL_URL;
            getCurrentUserAsInitParam = SilverPart.SilverPartWebPart.DEFAULT_GETCURRENTUSER;
            getCurrentSiteAsInitParam = SilverPart.SilverPartWebPart.DEFAULT_GETCURRENTSITE;
        }

        #endregion
    
        #region Public Properties

        //Property: Silverlight Control ID
        //Sets the "id" value in Silverlight.createObjectEx method
        public string SilverlightControlID
        {
            get
            {
                return silverlightControlID;
            }
            set
            {
                //Concatenate with control ID generated by ASP.NET to ensure the ID is unique on the page
                silverlightControlID = value;
            }
        }

        //Property: Silverlight Control Host
        //Sets the "parentElement" value in Silverlight.createObjectEx method
        public string SilverlightControlHost
        {
            get
            {
                return silverlightControlHost;
            }
            set
            {
                silverlightControlHost = value;
            }
        }

        //Property: Show Silverlight Install Banner
        //Show Install Image if plugin is not installed
        public bool ShowInstallBanner
        {
            get
            {
                return silverlightInstallBanner;
            }
            set
            {
                silverlightInstallBanner = value;
            }
        }

        //Property: Silverlight Install Banner Image
        //Install Image to be shown if plugin is not installed
        public string InstallBannerImage
        {
            get
            {
                return silverlightInstallImage;
            }
            set
            {
                silverlightInstallImage = value;
            }
        }

        //Property: Silverlight Install Banner URL
        //Install URL to download plugin if is not installed
        public string InstallBannerURL
        {
            get
            {
                return silverlightInstallURL;
            }
            set
            {
                silverlightInstallURL = value;
            }
        }
        
        //Property: XAML source
        //Sets the "source" value in Silverlight.createObjectEx method
        [Personalizable(PersonalizationScope.Shared),
         WebBrowsable(false)]
        public string SilverlightControlSource
        {
            get
            {
                return silverlightControlSource;
            }
            set
            {
                silverlightControlSource = value;
            }
        }

        //Property: JavaScript file
        //Sets the JavaScript file to be used by the Silverlight application
        [Personalizable(PersonalizationScope.Shared),
         WebBrowsable(false)]
        public string SilverlightControlJavaScriptFiles
        {
            get
            {
                return silverlightControlJavaScriptFile;
            }
            set
            {
                silverlightControlJavaScriptFile = value;
            }
        }

        //Property: Silverlight Control Height
        //Sets the "height" property value in Silverlight.createObjectEx method
        [Personalizable(PersonalizationScope.Shared),
         WebBrowsable(false)]
        public string SilverlightControlHeight
        {
            get
            {
                return silverlightControlHeight;
            }
            set
            {
                silverlightControlHeight = value;
            }
        }

        //Property: Silverlight Control Width
        //Sets the "width" property value in Silverlight.createObjectEx method
        [Personalizable(PersonalizationScope.Shared),
         WebBrowsable(false)]
        public string SilverlightControlWidth
        {
            get
            {
                return silverlightControlWidth;
            }
            set
            {
                silverlightControlWidth = value;
            }
        }

        //Property: Silverlight Control Version
        //Sets the "version" value in Silverlight.createObjectEx method
        [Personalizable(PersonalizationScope.Shared),
         WebBrowsable(false)]
        public string SilverlightControlVersion
        {
            get
            {
                return silverlightControlVersion;
            }

            set
            {
                silverlightControlVersion = value;
            }
        }

        //Property: Silverlight Control Background color
        //Sets the "background" property value in Silverlight.createObjectEx method
        [Personalizable(PersonalizationScope.Shared),
         WebBrowsable(false)]
        public string SilverlightControlBackground
        {
            get
            {
                return silverlightControlBackground;
            }
            set
            {
                silverlightControlBackground = value;
            }
        }

        //Property: Silverlight Control initParams property
        //Sets the "initParams" property value in Silverlight.createObjectEx method
        [Personalizable(PersonalizationScope.Shared),
         WebBrowsable(false)]
        public string SilverlightControlInitParams
        {
            get
            {
                return silverlightControlInitParams;
            }
            set
            {
                silverlightControlInitParams = value;
            }
        }

        //Property: Silverlight Control enableHtmlAccess property
        //Sets the "enableHtmlAccess" property value in Silverlight.createObjectEx method
        [Personalizable(PersonalizationScope.Shared),
         WebBrowsable(false)]
        public bool SilverlightControlHtmlAccess
        {
            get
            {
                return silverlightControlHtmlAccess;
            }
            set
            {
                silverlightControlHtmlAccess = value;
            }
        }

        //Property: Silverlight Control Windowless property
        //Sets the "Windowless" property value in Silverlight.createObjectEx method
        [Personalizable(PersonalizationScope.Shared), WebBrowsable(false)]
        public bool SilverlightControlWindowless
        {
            get
            {
                return silverlightControlWindowless;
            }
            set
            {
                silverlightControlWindowless = value;
            }
        }

        //Property: Silverlight Control Current User property
        //Pass the current SharePoint user as an Initialization parameter to the Silverlight control
        [Personalizable(PersonalizationScope.Shared),
         WebBrowsable(false)]
        public bool SilverlightControlCurrentUser
        {
            get
            {
                return getCurrentUserAsInitParam;
            }
            set
            {
                getCurrentUserAsInitParam = value;
            }
        }

        //Property: Silverlight Control Current Site property
        //Pass the current SharePoint site as an Initialization parameter to the Silverlight control
        [Personalizable(PersonalizationScope.Shared),
         WebBrowsable(false)]
        public bool SilverlightControlCurrentSite
        {
            get
            {
                return getCurrentSiteAsInitParam;
            }
            set
            {
                getCurrentSiteAsInitParam = value;
            }
        }
     
        #endregion

        #region Web Part Override Methods

        protected override void CreateChildControls()
        {
            if (string.IsNullOrEmpty(this.silverlightControlSource))
            {
                setWebPartSettings = new LiteralControl(string.Format("To set the Silverlight control settings <a href=\"javascript:MSOTlPn_ShowToolPaneWrapper('{0}','{1}','{2}');\">open the web part tool pane</a>.", 1, 129, this.ID));
            }
            else
            {
                setWebPartSettings = new LiteralControl();
            }

                //Ensure that unique IDs are assigned to these properties
                SilverlightControlID = "slControl_" + this.ClientID;
                SilverlightControlHost = "slHost_" + this.ClientID;
        }

        protected override void OnPreRender(EventArgs e)
        {
            //Register custom Javascript code-behind file(s) if specified
            if (!String.IsNullOrEmpty(SilverlightControlJavaScriptFiles))
            {
                string[] Jscripts = SilverlightControlJavaScriptFiles.Split(';');
                if (Jscripts.Length == 1)
                {
                    Page.ClientScript.RegisterClientScriptInclude(this.ClientID.ToString() + "_JS", SilverlightControlJavaScriptFiles);
                }
                else if (Jscripts.Length >= 1)
                {
                    int count = 1;
                    foreach (string s in Jscripts)
                    {
                        string js = s.Replace('\r', ' ').Replace('\n',' ').Trim();
                        Page.ClientScript.RegisterClientScriptInclude(this.ClientID.ToString() + "_JS" + count.ToString(), js);
                        count++;
                    }
                }

            }

            base.OnPreRender(e);

        }

        protected override void RenderContents(System.Web.UI.HtmlTextWriter htmlWriter)
        {
            try
            {
                base.EnsureChildControls();

                if (this.silverlightControlSource != String.Empty)
                {
                    if (this.silverlightControlVersion.ToUpper() == SL_VERSION10)
                    {
                        htmlWriter.Write(this.CreateSilverlightDivHostFactory(SilverlightVersion.Version1));
                    }
                    else if (this.silverlightControlVersion.ToUpper() == SL_VERSION20)
                    { 
                        htmlWriter.Write(this.CreateSilverlightDivHostFactory(SilverlightVersion.Version2));
                    }
                }
                else
                {
                    setWebPartSettings.RenderControl(htmlWriter);
                }
            }
            catch (Exception ex)
            {
                setWebPartSettings.Text = "An error occurred loading this webpart: " + ex.Message + "<BR>" + ex.StackTrace;
                setWebPartSettings.RenderControl(htmlWriter);
            }
        }

        //Use a custom Web Part Editor
        public override EditorPartCollection CreateEditorParts()
        {
            List<EditorPart> editorParts = new List<EditorPart>(1);

            //Instance of Custom Editor Part 
            EditorPart part = new SilverPartEditorPart();
            part.ID = this.ID + "_SilverEditorPart";
            editorParts.Add(part);

            return new EditorPartCollection(base.CreateEditorParts(), editorParts);

        }

        #endregion

    }

}